package com.itheima.controller;

import com.itheima.entity.Result;
import com.itheima.pojo.Admin;
import com.itheima.service.AdminService;
import com.itheima.service.CategoryMenuService;
import com.itheima.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * Description:
 *
 * @author WangChang
 * @date 2020/8/6 14:27
 */
@RestController
@RequestMapping("/auth")
@CrossOrigin(allowCredentials="true", allowedHeaders="*", methods={RequestMethod.GET,RequestMethod.POST}, origins="*")
public class AuthContorller {

    @Autowired
    private AdminService adminService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private CategoryMenuService categoryMenuService;


    private static  final String TOKEN_HEARDE = "LOGIN_TONLEN_";

    @PostMapping(value = "/login")
    public Result login(@RequestParam Map<String,String> map){
        String username = map.get("username");
        String password = map.get("password");

        if ("".equals(username)||"".equals(password)||username==null||password==null){
            return new Result("error", "账号或密码不能为空");
        }
        Admin admin = adminService.selectAdminByUsername(username);
        if (admin==null){
            return new Result("error", "用户不存在");
        }
        String passWord = admin.getPassWord();

        boolean matches = new BCryptPasswordEncoder().matches(password, passWord);
        if (!matches){
            return new Result("error", "用户名或密码错误");
        }

        //登录成功，升成token并返回
        String token = JwtUtil.createJWT(username, 100L, true, 1L);
        redisTemplate.boundValueOps(TOKEN_HEARDE+token).set(token,10, TimeUnit.MINUTES);
        HashMap<String, String> map1 = new HashMap<>();
        map1.put("token",token);

        return new Result("success", map1);
    }

    @GetMapping("/info")
    public Result info(){


        List<Map<String,String>> list = new ArrayList<>();
        Admin admin = adminService.selectAdminByUsername("admin");

        Map<String, String> listmap = new HashMap<>();
        Map<String, Object> map = new HashMap<>();
        listmap.put("roleName", "超级管理员");
        listmap.put("summary", "超级管理员，管理全部菜单和功能");

        listmap.put("uid", admin.getUid());
        listmap.put("status", admin.getStatus()+"");
//        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
//        String createTime = simpleDateFormat.format(((Date) admin.getCreateTime());
//        String updateTime = simpleDateFormat.format(admin.getUpdateTime());
        listmap.put("createTime", admin.getCreateTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        listmap.put("updateTime",admin.getUpdateTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));

        list.add(listmap);

        map.put("roles",list);

        return new Result("success",map);
    }

    @GetMapping("/getMenu")
    public Result getMenu(){
        Map<String ,Object> map = new HashMap<>();
        map.put("sonList",categoryMenuService.getMenulevel(2));
        map.put("buttonList",categoryMenuService.getbutten());
        map.put("parentList",categoryMenuService.getMenulevel(1));
        return new Result ("success",map);
    }
}